package jmu.edu.yjc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jmu.edu.yjc.Utils.BeanCopyUtil;
import jmu.edu.yjc.constants.SystemConstants;
import jmu.edu.yjc.domain.ResponseResult;
import jmu.edu.yjc.domain.dto.RoleDto;
import jmu.edu.yjc.domain.entity.Role;
import jmu.edu.yjc.domain.entity.RoleMenu;
import jmu.edu.yjc.domain.vo.PageVo;
import jmu.edu.yjc.mapper.RoleMapper;
import jmu.edu.yjc.service.RoleMenuService;
import jmu.edu.yjc.service.RoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色信息表(Role)表服务实现类
 *
 * @author makejava
 * @since 2023-01-19 16:44:14
 */
@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Resource
    private RoleMenuService roleMenuService;
    @Override
    public List<String> selectRoleKeyByUserId(Long userId) {
        //判断是否是管理员如果是返回集合中只要有admin
        if(userId==1L){
            List<String> roleKeys = new ArrayList<>();
            roleKeys.add("admin");
            return roleKeys;
        }
        //否则查询用户所具有的用户信息
        return getBaseMapper().selectRoleKeyByUserId(userId);
    }

    @Override
    public ResponseResult listRole(String roleName, String status, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Role> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //目前没有根据id查询
//        lambdaQueryWrapper.eq(Objects.nonNull(role.getId()),Role::getId,role.getId());
        lambdaQueryWrapper.like(StringUtils.hasText(roleName),Role::getRoleName,roleName);
        lambdaQueryWrapper.eq(StringUtils.hasText(status),Role::getStatus,status);
        lambdaQueryWrapper.orderByAsc(Role::getRoleSort);

        Page<Role> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        page(page,lambdaQueryWrapper);

        //转换成VO
        List<Role> roles = page.getRecords();

        PageVo pageVo = new PageVo();
        pageVo.setTotal(page.getTotal());
        pageVo.setRows(roles);
        return ResponseResult.okResult(pageVo);
    }
    @Transactional
    @Override
    public ResponseResult addRole(RoleDto role) {
        Role role1 = BeanCopyUtil.copyBeans(role, Role.class);
        save(role1);
        role.setId(role1.getId());
        if(role.getMenuIds().size()>0) {
            insertRoleMenu(role);
        }
        return ResponseResult.okResult();
    }
    @Transactional
    @Override
    public void updateRole(RoleDto role) {
        Role role1 = BeanCopyUtil.copyBeans(role, Role.class);
        updateById(role1);
        //把原来的删除
        roleMenuService.deleteRoleMenuByRoleId(role.getId());
        //插入新的
        insertRoleMenu(role);
    }

    @Override
    public ResponseResult listAllRole() {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Role::getStatus, SystemConstants.STATUS_NORMAL);
        List<Role> list = list(queryWrapper);
        return ResponseResult.okResult(list);
    }

    public void insertRoleMenu(RoleDto role) {
        List<RoleMenu> roleMenuList = role.getMenuIds().stream()
                .map(menuId -> new RoleMenu(role.getId(), menuId))
                .collect(Collectors.toList());
        roleMenuService.saveBatch(roleMenuList);
    }
}
